package org.uninova.mobis.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.uninova.mobis.api.interfaces.open.routers.RoutinoInterface;

/**
 * Servlet implementation class GetRouteInstructionsRoutinoServlet
 * @author PAF
 */
@WebServlet("/GetRouteInstructionsRoutinoServlet")
public class GetRouteInstructionsRoutinoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetRouteInstructionsRoutinoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response) ;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response) ;
	}
	
	/**
	 * This servlet has the purpose of read the HTML file created by Routino, containing the route instructions
	 * 
	 * @param shortest		Boolean value stating if the user wants the shortest (true) or fastest (false) route
	 * 
	 * @return				HTML instructions created by Routino for the calculated route (TODO: Change from HTML to JSON format)
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
		boolean shortest = Boolean.parseBoolean(request.getParameter("shortest")) ;
		response.setContentType("text/html;charset=UTF-8");
		RoutinoInterface routinoInterface = new RoutinoInterface() ;
		try {
			PrintWriter out = response.getWriter();
			String instructions = routinoInterface.readRouttinoInstructions(shortest) ;
			out.println(instructions) ;
			out.close() ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
